package com.certification.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.certification.common.utils.Constant;
import com.certification.dto.ProfessionSelfEvaluationDTO;
import com.certification.entity.ProfessionSelfEvaluation;
import com.certification.service.ProfessionSelfEvaluationService;
import com.certification.service.SysUserRoleService;
import com.certification.vo.FindCertVO;
import com.certification.vo.ProfessionSelfEvaluationVO;
import com.certification.vo.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-03-29 21:16
 * @function
 */
@RestController
@Api(tags = {"流程：专业自评"})
public class ProfessionSelfEvaluationController extends AbstractController {

    @Resource
    private ProfessionSelfEvaluationService professionSelfEvaluationService;

    @Resource
    private SysUserRoleService sysUserRoleService;

//    @PostMapping("pro")
//    @ApiOperation(value = "新增指标")
//    public ResponseMessage<String> addPro(@RequestBody ProfessionSelfEvaluationDTO professionSelfEvaluationDTO) {
//        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
//            if (roleId == Constant.ROLE_PRINCIPAL) {
//                return professionSelfEvaluationService.addPro(professionSelfEvaluationDTO, Math.toIntExact(getUserId()));
//            }
//        }
//        return ResponseMessage.errorMsg("该角色不能调用该接口~");
//    }

    @PostMapping("pro")
    @ApiOperation(value = "新增指标")
    public ResponseMessage<String> addPro(@RequestBody List<ProfessionSelfEvaluationDTO> professionSelfEvaluationDTOList) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return professionSelfEvaluationService.addProList(professionSelfEvaluationDTOList, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("proPage")
    @ApiOperation(value = "查看专业自评分页")
    public ResponseMessage<IPage<ProfessionSelfEvaluationVO>> getProPage(FindCertVO findProVO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return professionSelfEvaluationService.getProPage(Math.toIntExact(getUserId()), findProVO);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("pro")
    @ApiOperation(value = "查看专业自评")
    public ResponseMessage<List<ProfessionSelfEvaluationVO>> getPro() {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return professionSelfEvaluationService.getPro(Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("pro/{proId}")
    @ApiOperation(value = "查看专业自评详情")
    public ResponseMessage<ProfessionSelfEvaluation> getProDetail(@PathVariable("proId") Integer proId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return professionSelfEvaluationService.getProDetail(proId, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @PutMapping("pro")
    @ApiOperation(value = "修改指标")
    public ResponseMessage<ProfessionSelfEvaluation> putPro(@RequestBody ProfessionSelfEvaluationDTO professionSelfEvaluationDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return professionSelfEvaluationService.putPro(professionSelfEvaluationDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

}
